package com.glodon.paas.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DateUtil {	
	public static final Logger LOGGER = LoggerFactory.getLogger(DateUtil.class);
	
	 //缺省的日期格式
	 public static final String DAFAULT_DATE_FORMAT = "yyyy-MM-dd";
	 //缺省的日期时间格式
	 public static final String DAFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	 //缺省的时间格式
	 public static final String DAFAULT_TIME_FORMAT = "HH:mm:ss";
	 
	 
	 //将日期类型转换成数字类型
	 public static final String LONG_DATETIME_FORMAT = "yyyyMMddHHmmss";
	 
	 //默认日期,时间 格试化对象
	 private static SimpleDateFormat defaultDateFormat = new SimpleDateFormat(DAFAULT_DATE_FORMAT);
	 private static SimpleDateFormat defaultDatetimeFormat = new SimpleDateFormat(DAFAULT_DATETIME_FORMAT);
	 private static SimpleDateFormat timeFormat = new SimpleDateFormat(DAFAULT_TIME_FORMAT);
	 private static final SimpleDateFormat longDateTimeFormat = new SimpleDateFormat(LONG_DATETIME_FORMAT);

	 private DateUtil()
	 {
	     // 私用构造主法.因为此类是工具类.
	 }

	 /**
	  * 获取格式化实例.
	  *
	  * @param pattern
	  *            如果为空使用DAFAULT_DATE_FORMAT
	  * @return
	  */
	 public static SimpleDateFormat getFormatInstance(String pattern) {
	     if (pattern == null || pattern.trim().length() == 0){
	         pattern = DAFAULT_DATE_FORMAT;
	     }
	     return new SimpleDateFormat(pattern);
	 }

	 /**
	  * 格式化Calendar
	  *
	  * @param calendar
	  * @return
	  */
	 public static String formatCalendar(Calendar calendar) {
	     if (calendar == null)
	     {
	         return "";
	     }
	     return defaultDateFormat.format(calendar.getTime());
	 }

	 public static String formatDateTime(Date d) {
	     if (d == null)
	     {
	         return "";
	     }
	     return defaultDatetimeFormat.format(d);
	 }

	 public static String formatDate(Date d) {
	     if (d == null)
	     {
	         return "";
	     }	            
	     return defaultDateFormat.format(d);
	 }

	 /**
	  * 格式化时间
	  *
	  * @param calendar
	  * @return
	  */
	 public static String formatTime(Date d) {
	     if (d == null)
	     {
	         return "";
	     }
	     return timeFormat.format(d);
	 }

	 /**
	  * 格式化整数型日期
	  *
	  * @param intDate
	  * @return
	  */
	 public static String formatIntDate(Integer intDate) {
	     if (intDate == null)
	     {
	         return "";
	     }
	     Calendar c = newCalendar(intDate);
	     return formatCalendar(c);
	 }

	 /**
	  * 根据指定格式化来格式日期.
	  *
	  * @param date
	  *            待格式化的日期.
	  * @param pattern
	  *            格式化样式或分格,如yyMMddHHmmss
	  * @return 字符串型日期.
	  */
	 public static String formatDate(Date date, String pattern) {
	     if (date == null)
	     {
	         return "";
	     }
	     if (StringUtils.isBlank(pattern))
	     {
	         return formatDate(date);
	     }
	     SimpleDateFormat simpleDateFormat = null;
	     try
	     {
	         simpleDateFormat = new SimpleDateFormat(pattern);
	     } catch (Exception e)
	     {
	         e.printStackTrace();
	         return formatDate(date);
	     }
	     return simpleDateFormat.format(date);
	 }

	 /**
	  * 取得Integer型的当前日期
	  *
	  * @return
	  */
	 public static Integer getIntNow() {
	     return getIntDate(getNow());
	 }

	 /**
	  * 取得Integer型的当前日期
	  *
	  * @return
	  */
	 public static Integer getIntToday() {
	     return getIntDate(getNow());
	 }

	 /**
	  * 取得Integer型的当前年份
	  *
	  * @return
	  */
	 public static Integer getIntYearNow() {
	     Calendar c = Calendar.getInstance();
	     int year = c.get(Calendar.YEAR);
	     return year;
	 }

	 /**
	  * 取得Integer型的当前月份
	  *
	  * @return
	  */
	 public static Integer getIntMonthNow() {
	     Calendar c = Calendar.getInstance();
	     int month = c.get(Calendar.MONTH) + 1;
	     return month;
	 }

	 public static String getStringToday() {
	     return getIntDate(getNow()) + "";
	 }

	 /**
	  * 根据年月日获取整型日期
	  *
	  * @param year
	  * @param month
	  * @param day
	  * @return
	  */
	 public static Integer getIntDate(int year, int month, int day) {
	     return getIntDate(newCalendar(year, month, day));
	 }

	 /**
	  * 某年月的第一天
	  *
	  * @param year
	  * @param month
	  * @return
	  */
	 public static Integer getFirstDayOfMonth(int year, int month) {
	     return getIntDate(newCalendar(year, month, 1));
	 }

	 /**
	  * 某年月的第一天
	  *
	  * @param year
	  * @param month
	  * @return
	  */
	 public static Integer getFirstDayOfThisMonth() {
	     Integer year = DateUtil.getIntYearNow();
	     Integer month = DateUtil.getIntMonthNow();
	     return getIntDate(newCalendar(year, month, 1));
	 }

	 /**
	  * 某年月的第一天
	  *
	  * @param date
	  * @return
	  * @time:2008-7-4 上午09:58:55
	  */
	 public static Integer getFistDayOfMonth(Date date) {
	     Integer intDate = getIntDate(date);
	     int year = intDate / 10000;
	     int month = intDate % 10000 / 100;
	     return getIntDate(newCalendar(year, month, 1));
	 }

	 /**
	  * 某年月的最后一天
	  *
	  * @param year
	  * @param month
	  * @return
	  */
	 public static Integer getLastDayOfMonth(int year, int month) {
	     return intDateSub(getIntDate(newCalendar(year, month + 1, 1)), 1);
	 }

	 /**
	  * 根据Calendar获取整型年份
	  *
	  * @param c
	  * @return
	  */
	 public static Integer getIntYear(Calendar c) {
	     int year = c.get(Calendar.YEAR);
	     return year;
	 }

	 /**
	  * 根据Calendar获取整型日期
	  *
	  * @param c
	  * @return
	  */
	 public static Integer getIntDate(Calendar c) {
	     int year = c.get(Calendar.YEAR);
	     int month = c.get(Calendar.MONTH) + 1;
	     int day = c.get(Calendar.DAY_OF_MONTH);
	     return year * 10000 + month * 100 + day;
	 }

	 /**
	  * 根据Date获取整型年份
	  *
	  * @param d
	  * @return
	  */
	 public static Integer getIntYear(Date d) {
	     if (d == null)
	     {
	         return null;
	     }
	     Calendar c = Calendar.getInstance();
	     c.setTime(d);
	     return getIntYear(c);
	 }

	 /**
	  * 根据Date获取整型日期
	  *
	  * @param d
	  * @return
	  */
	 public static Integer getIntDate(Date d) {
	     if (d == null)
	     {
	         return null;
	     }
	     Calendar c = Calendar.getInstance();
	     c.setTime(d);
	     return getIntDate(c);
	 }

	 /**
	  * 根据Integer获取Date日期
	  *
	  * @param n
	  * @return
	  */
	 public static Date getDate(Integer n) {
	     if (n == null)
	     {
	         return null;
	     }
	     Calendar c = Calendar.getInstance();
	     c.set(n / 10000, n / 100 % 100 - 1, n % 100);
	     return c.getTime();
	 }

	 public static Date getDate(String date) {
	     if (date == null || date.length() == 0)
	     {
	         return null;
	     }

	     try
	     {
	         if (date.contains("/"))
	         {
	             date = date.replaceAll("/", "-");
	         }
	         return getFormatInstance(DAFAULT_DATE_FORMAT).parse(date);
	     } catch (Exception e)
	     {
	    	 LOGGER.error("解析[" + date + "]错误！", e);
	         return null;
	     }
	 }

	 public static Date getDate(String date, String format) {
	     try
	     { 
	    	 return getFormatInstance(format).parse(date);
	         
	     } catch (Exception e)
	     {
	    	 LOGGER.error("解析[" + date + "]错误！", e);
	         return null;
	     }
	 }
	 
	 /**
	  * 根据年份Integer获取Date日期
	  *
	  * @param year
	  * @return
	  */
	 public static Date getFirstDayOfYear(Integer year) {
	     if (year == null)
	     {
	         return null;
	     }
	     Calendar c = Calendar.getInstance();
	     c.set(year, 1, 1);
	     return c.getTime();
	 }

	 /**
	  * 根据年月日生成Calendar
	  *
	  * @param year
	  * @param month
	  * @param day
	  * @return
	  */
	 public static Calendar newCalendar(int year, int month, int day) {
	     Calendar ret = Calendar.getInstance();
	     if (year < 100)
	     {
	         year = 2000 + year;
	     }
	     ret.set(year, month - 1, day);
	     return ret;
	 }

	 /**
	  * 根据整型日期生成Calendar
	  *
	  * @param date
	  * @return
	  */
	 public static Calendar newCalendar(int date) {
	     int year = date / 10000;
	     int month = (date % 10000) / 100;
	     int day = date % 100;

	     Calendar ret = Calendar.getInstance();
	     ret.set(year, month - 1, day);
	     return ret;
	 }

	 /**
	  * 取得Date型的当前日期
	  *
	  * @return
	  */
	 public static Date getNow() {
	     return new Date();
	 }

	 /**
	  * 取得Date型的当前日期
	  *
	  * @return
	  */
	 public static Date getToday() {
	     return DateUtil.getDate(DateUtil.getIntToday());
	 }

	 /**
	  * 整数型日期的加法
	  *
	  * @param date
	  * @param days
	  * @return
	  */
	 public static Integer intDateAdd(int date, int days) {
	     int year = date / 10000;
	     int month = (date % 10000) / 100;
	     int day = date % 100;

	     day += days;

	     return getIntDate(year, month, day);
	 }

	 /**
	  * 整数型日期的减法
	  *
	  * @param date
	  * @param days
	  * @return
	  */
	 public static Integer intDateSub(int date, int days) {
	     return intDateAdd(date, -days);
	 }

	 /**
	  * 计算两个整型日期之间的天数
	  *
	  * @param startDate
	  * @param endDate
	  * @return
	  */
	 public static Integer daysBetweenDate(Integer startDate, Integer endDate) {
	     if (startDate == null || endDate == null)
	     {
	         return null;
	     }
	     Calendar c1 = newCalendar(startDate);
	     Calendar c2 = newCalendar(endDate);

	     Long lg = (c2.getTimeInMillis() - c1.getTimeInMillis()) / 1000 / 60 / 60 / 24;
	     return lg.intValue();
	 }

	 /**
	  * 计算两个整型日期之间的天数
	  *
	  * @param startDate
	  * @param endDate
	  * @return
	  */
	 public static Integer daysBetweenDate(Date startDate, Date endDate) {
	     if (startDate == null || endDate == null)
	     {
	         return null;
	     }
	     Long interval = endDate.getTime() - startDate.getTime();
	     interval = interval / (24 * 60 * 60 * 1000);
	     return interval.intValue();
	 }

	 /**
	  * 计算两个整型日期之间的分钟数
	  *
	  * @param startDate
	  * @param endDate
	  * @return
	  */
	 public static Integer minBetweenDate(Date startDate, Date endDate) {
	     if (startDate == null || endDate == null)
	     {
	         return null;
	     }
	     long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
		 long nh = 1000 * 60 * 60;// 一小时的毫秒数
		 long nm = 1000 * 60;// 一分钟的毫秒数
	     Long interval = endDate.getTime() - startDate.getTime();
	     interval= interval.longValue() % nd % nh / nm;// 计算差多少分钟
	     return interval.intValue();
	 }
	 
	 /**
	  * 取得当前日期.
	  *
	  * @return 当前日期,字符串类型.
	  */
	 public static String getStringDate() {
	     return getStringDate(DateUtil.getNow());
	 }

	 /**
	  * 根据calendar产生字符串型日期
	  *
	  * @param d
	  * @return eg:20080707
	  */
	 public static String getStringDate(Date d) {
	     if (d == null)
	     {
	         return "";
	     }
	     SimpleDateFormat sdf = new SimpleDateFormat(DAFAULT_DATE_FORMAT);
	     return sdf.format(d);
	 }
	 

	 public static String getStringDate(Date d, String format) {
	     if (d == null)
	     {
	         return "";
	     }
	     SimpleDateFormat sdf = getFormatInstance(format);
	     return sdf.format(d);
	 }
	 
	 /**
	  * 取得当前日期.
	  *
	  * @return 当前日期,字符串类型.
	  */
	 public static String getStringTime() {
	     return formatDateTime(DateUtil.getNow());
	 }
	 
	 /**
	  * 获取可读Long类型的日期字符串，如20121221010101
	  * @param date
	  * @return
	  */
	 public static Long readableLongDateTime(Date date) {
		 String str = longDateTimeFormat.format(date);
	     return Long.valueOf(str);
	 }
	 
	 public static String getIntervalDate(int interval){
		 Calendar cal = Calendar.getInstance();
		 cal.add(Calendar.DATE,interval);
		 String intervalDate = new SimpleDateFormat(DAFAULT_DATETIME_FORMAT).format(cal.getTime());
		 return intervalDate;
	 }
	 
	 public static String getIntervalHour(int interval){
		 Calendar cal = Calendar.getInstance();
		 cal.add(Calendar.HOUR_OF_DAY,interval);
		 String intervalDate = new SimpleDateFormat(DAFAULT_DATETIME_FORMAT).format(cal.getTime());
		 return intervalDate;
	 }
	 
	 public static void main(String[] args) {
		System.out.println(getIntervalHour(-1));
	}
}
